<?php

namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

/*
 * 数据模型层，负责技术层面上对数据信息的提取、存储、更新和删除等操作，数据可来自内存，也可以来自持久化存储媒介，甚至可以是来自外部第三方系统
 */

class Arch extends NotORM {

    public function getTableName($id) {
        return 'archives';
    }

    public function getAll() {

        return $this->getORM()
                        ->select('id,typeid,sortrank,flag,channel,click,title,litpic,pubdate,description,weight')
                        ->where(['arcrank' => 0])
                        ->fetchAll();
    }

    public function getOne($id) {

        $sql = "SELECT a.id,a.typeid,a.sortrank,a.flag,a.channel,a.click,a.title,a.litpic,a.pubdate,a.description,a.weight, b.url,b.filesize,b.uptime "
                . "FROM dede_archives AS a LEFT JOIN dede_uploads AS b "
                . "ON a.id = b.arcid "
                . "WHERE a.id={$id} AND a.arcrank=0 AND b.mediatype=0 "
                . "ORDER BY b.aid ASC";

        return $this->getORM()->queryAll($sql, array());
    }

    public function getBatch($tid, $cate, $keyword) {

        if (!empty($keyword)) {
            $sql = "SELECT id,typeid,sortrank,flag,channel,click,title,litpic,pubdate,description,weight FROM dede_archives WHERE arcrank=0 AND title LIKE '%$keyword%'";
        } else {
            if (!empty($tid) && empty($cate)) {
                $sql = "SELECT a.id,a.typeid,a.sortrank,a.flag,a.channel,a.click,a.title,a.litpic,a.pubdate,a.description,a.weight, b.url,b.filesize,b.uptime "
                        . "FROM dede_archives AS a LEFT JOIN dede_uploads AS b "
                        . "ON a.id = b.arcid "
                        . "WHERE a.typeid={$tid} AND a.arcrank=0 AND b.mediatype=0 "
                        . "ORDER BY a.id DESC,b.aid ASC";
            } else if (empty($tid) && !empty($cate)) {
                $sql = "SELECT id,typeid,sortrank,flag,channel,click,title,litpic,pubdate,description,weight FROM dede_archives WHERE FIND_IN_SET('$cate',flag)";
            } else if (!empty($tid) && !empty($cate)) {
                $sql = "SELECT id,typeid,sortrank,flag,channel,click,title,litpic,pubdate,description,weight FROM dede_archives WHERE FIND_IN_SET('$cate',flag) AND typeid=$tid";
            }
        }

        return $this->getORM()->queryAll($sql, array());
    }

}
